import 'package:finance_app/src/entities/setting_info_entity.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';

settingInfoEntityFromJson(SettingInfoEntity data, Map<String, dynamic> json) {
	if (json['language'] != null) {
		data.language = json['language'].toString();
	}
	if (json['default_currency_code'] != null) {
		data.defaultCurrencyCode = json['default_currency_code'].toString();
	}
	if (json['default_payment_type_id'] != null) {
		data.defaultPaymentTypeId = json['default_payment_type_id'];
	}
	if (json['default_payment_term_id'] != null) {
		data.defaultPaymentTermId = json['default_payment_term_id'];
	}
	if (json['tax_id'] != null) {
		data.taxId = json['tax_id'];
	}
	if (json['date_format'] != null) {
		data.dateFormat = json['date_format'].toString();
	}
	if (json['number_formats'] != null) {
		data.numberFormats = (json['number_formats'] as List).map((v) => SettingInfoNumberFormats().fromJson(v)).toList();
	}
	if (json['templates'] != null) {
		data.templates = SettingInfoTemplates().fromJson(json['templates']);
	}
	if (json['unit_types'] != null) {
		data.unitTypes = (json['unit_types'] as List).map((v) => SettingDataUnitType().fromJson(v)).toList();
	}
	if (json['alerts'] != null) {
		data.alerts = (json['alerts'] as List).map((v) => v.toString()).toList().cast<String>();
	}
	return data;
}

Map<String, dynamic> settingInfoEntityToJson(SettingInfoEntity entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['language'] = entity.language;
	data['default_currency_code'] = entity.defaultCurrencyCode;
	data['default_payment_type_id'] = entity.defaultPaymentTypeId;
	data['default_payment_term_id'] = entity.defaultPaymentTermId;
	data['tax_id'] = entity.taxId;
	data['date_format'] = entity.dateFormat;
	data['number_formats'] =  entity.numberFormats?.map((v) => v.toJson())?.toList();
	data['templates'] = entity.templates?.toJson();
	data['unit_types'] =  entity.unitTypes?.map((v) => v.toJson())?.toList();
	data['alerts'] = entity.alerts;
	return data;
}

settingInfoNumberFormatsFromJson(SettingInfoNumberFormats data, Map<String, dynamic> json) {
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['type'] != null) {
		data.type = json['type'].toString();
	}
	if (json['prefix'] != null) {
		data.prefix = json['prefix'].toString();
	}
	if (json['digit'] != null) {
		data.digit = json['digit'].toString();
	}
	if (json['number'] != null) {
		data.number = json['number'] is String
				? int.tryParse(json['number'])
				: json['number'].toInt();
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	return data;
}

Map<String, dynamic> settingInfoNumberFormatsToJson(SettingInfoNumberFormats entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['type'] = entity.type;
	data['prefix'] = entity.prefix;
	data['digit'] = entity.digit;
	data['number'] = entity.number;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	return data;
}

settingInfoTemplatesFromJson(SettingInfoTemplates data, Map<String, dynamic> json) {
	if (json['invoice'] != null) {
		data.invoice = json['invoice'].toString();
	}
	if (json['delivery'] != null) {
		data.delivery = json['delivery'].toString();
	}
	if (json['quotation'] != null) {
		data.quotation = json['quotation'].toString();
	}
	if (json['credit_note'] != null) {
		data.creditNote = json['credit_note'].toString();
	}
	if (json['purchase_order'] != null) {
		data.purchaseOrder = json['purchase_order'].toString();
	}
	return data;
}

Map<String, dynamic> settingInfoTemplatesToJson(SettingInfoTemplates entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['invoice'] = entity.invoice;
	data['delivery'] = entity.delivery;
	data['quotation'] = entity.quotation;
	data['credit_note'] = entity.creditNote;
	data['purchase_order'] = entity.purchaseOrder;
	return data;
}